package com.boco.modules.fdoc.web.outpatient;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.parser.deserializer.DateFormatDeserializer;
import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.BizOutpatientOrderEntity;
import com.boco.modules.fdoc.model.BizOutpatientStopEntity;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.service.OutpatientStopService;
import com.boco.modules.fdoc.vo.OutpatientStopVo;

/**
 * Description：停诊管理(暂时未添加消息推送和短信发送)
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/outpatientStop",produces = "application/json;charset=UTF-8")
public class OutpatientStopController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	OutpatientStopService stopService;
	
	/**
	 * 跳转停诊主页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/outpatient/outpatientStop";
	}
	
	/**
	 * 获取停诊通知列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param startTime 查询开始时间
	 * @param endTime 查询结束时间
	 * @param pageIndex 页码
	 * @param pageSize 页面大小
	 * @return
	 * @throws ParseException 日期格式转换异常
	 */
	@ResponseBody
	@RequestMapping(value="getStopList",method=RequestMethod.GET)
	public String getStopList(HttpServletRequest request, Model model,String orgId,String  startTime,String  endTime,
				Integer pageIndex,Integer pageSize) throws ParseException{
		OutpatientStopVo stopVo=new OutpatientStopVo();
		OutpatientStopVo tstopVo=new OutpatientStopVo();
		Page<OutpatientStopVo> page=new Page<OutpatientStopVo>(pageIndex, pageSize);
		stopVo.setPage(page);
		//查询没选择机构及时间区间的数据
		if((endTime==null || "".equals(endTime))&&(orgId==null||"".equals(orgId))){
			List<OutpatientStopVo> list=stopService.getStopList(stopVo);
			int count=stopService.getCount(tstopVo);
			return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
		}else if((orgId!=null&&!("".equals(orgId)))&&(endTime==null || "".equals(endTime))){
			//查询选择机构及没有时间区间的数据
			stopVo.setOrgId(orgId);
			List<OutpatientStopVo> list=stopService.getStopList(stopVo);
			tstopVo.setOrgId(orgId);
			int count=stopService.getCount(tstopVo);
			return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
		}else{
			//查询选择机构及时间区间的数据
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			
			Date date1=sdf.parse(startTime);
			Calendar stTime=Calendar.getInstance();
			stTime.set(Calendar.YEAR, date1.getYear()+1900);
			stTime.set(Calendar.MONTH, date1.getMonth());
			stTime.set(Calendar.DAY_OF_MONTH,date1.getDate());
			stTime.set(Calendar.HOUR_OF_DAY, 0);  
			stTime.set(Calendar.MINUTE, 0);  
			stTime.set(Calendar.SECOND, 0);
			stTime.set(Calendar.MILLISECOND, 0);
			Date sdate=new Date(stTime.getTime().getTime());
			
			Date date2=sdf.parse(endTime);
			Calendar edTime=Calendar.getInstance();
			edTime.set(Calendar.YEAR, date2.getYear()+1900);
			edTime.set(Calendar.MONTH, date2.getMonth());
			edTime.set(Calendar.DAY_OF_MONTH,date2.getDate());
			edTime.set(Calendar.HOUR_OF_DAY, 23);  
			edTime.set(Calendar.MINUTE, 59);  
			edTime.set(Calendar.SECOND, 59);
			edTime.set(Calendar.MILLISECOND, 999);
			Date edate=new Date(edTime.getTime().getTime());
			
			stopVo.setStartTime(sdate);
			stopVo.setEndTime(edate);
			stopVo.setOrgId(orgId);
			List<OutpatientStopVo> list=stopService.getStopList(stopVo);
			tstopVo.setStartTime(sdate);
			tstopVo.setEndTime(edate);
			tstopVo.setOrgId(orgId);
			int count=stopService.getCount(tstopVo);
			
			return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
		}
	}
	
	/**
	 * 跳转新增停诊通知页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toStop",method = RequestMethod.GET)
	public String toStop(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		Date date=new Date();
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, 1);
		
		model.addAttribute("today",dateFormat.format(calendar.getTime()));
		return "/outpatient/addStop";
	}
	
	/**
	 * 
	 * 新增停诊
	 * @param request
	 * @param model
	 * @param updateMan 修改人
	 * @param orgId 医院id
	 * @param startTime 停诊开始时间
	 * @param endTime 停诊结束时间
	 * @param stopTime 停诊时间段
	 * @param notifyWay 提醒方式
	 * @param stopReason 停诊原因
	 * @return
	 * @throws ParseException
	 */
	@Transactional
	@ResponseBody
	@RequestMapping(value="/addStop",method=RequestMethod.POST)
	public String addStop(HttpServletRequest request, Model model,String updateMan,String orgId,String  startTime,String  endTime,
			String stopTime,String notifyWay,String stopReason) throws ParseException{
		
		BizOutpatientStopEntity stopEntity=new BizOutpatientStopEntity();
		stopEntity.setOrgId(orgId);
		stopEntity.setStopTime(stopTime);
		stopEntity.setNotifyWay(notifyWay);
		stopEntity.setStopReason(stopReason);
		stopEntity.setUpdateTime(new Date());
		stopEntity.setCreateTime(new Date());
		stopEntity.setUpdateBy(updateMan);
		stopEntity.setCreateBy(updateMan);
		int flag=1;
		i:while(true){
			if(startTime.equals(endTime)){
				SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
				Date dateOne= dateFormat.parse(startTime);
				stopEntity.setStopDate(dateOne);
				flag=stopService.addStop(stopEntity);
			}
			else{
				SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
				Date dateOne= dateFormat.parse(startTime);
				Date dateTwo=dateFormat.parse(endTime);
				Calendar calendar=Calendar.getInstance();
				calendar.setTime(dateOne);
				
				while(calendar.getTime().before(dateTwo)){
					
					Date edate=new Date(calendar.getTime().getTime());
					stopEntity.setStopDate(edate);
					flag=stopService.addStop(stopEntity);
					
					calendar.add(Calendar.DAY_OF_MONTH, 1);
					if(flag<=0){
						break i;
					}
				}
				stopEntity.setStopDate(dateTwo);
				flag=stopService.addStop(stopEntity);
			}
			break;
		}
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 删除停诊通知
	 * @param request
	 * @param model
	 * @param id 停诊id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteStop",method=RequestMethod.POST)
	public String deleteStop(HttpServletRequest request, Model model,String id){
		
		int flag=stopService.deleteStop(Integer.parseInt(id));
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 通过id 获取停诊信息
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getStopbyId",method=RequestMethod.GET)
	public String getStopbyId(HttpServletRequest request, Model model,String id){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		BizOutpatientStopEntity stopEntity = new BizOutpatientStopEntity(); 
		stopEntity=stopService.getStopbyId(Integer.parseInt(id));
		model.addAttribute("stopEntity", stopEntity);
		return "/outpatient/updateStop";
	}
	
	/**
	 * 修改停诊信息
	 * @param request
	 * @param model
	 * @param id 停诊id
	 * @param updateMan 修改人
	 * @param orgId 医院id
	 * @param stopDate 停诊日期
	 * @param stopTime 停诊时间段
	 * @param notifyWay 提醒方式
	 * @param stopReason 停诊原因
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value="/updateStop",method=RequestMethod.POST)
	public String updateStop(HttpServletRequest request, Model model,String id,String updateMan,String orgId,String stopDate,
			String stopTime,String notifyWay,String stopReason) throws ParseException{
		
		BizOutpatientStopEntity stopEntity=new BizOutpatientStopEntity();
		stopEntity.setId(Integer.parseInt(id));
		stopEntity.setUpdateBy(updateMan);
		stopEntity.setUpdateTime(new Date());
		stopEntity.setOrgId(orgId);
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		stopEntity.setStopDate(dateFormat.parse(stopDate));
		stopEntity.setStopTime(stopTime);
		stopEntity.setNotifyWay(notifyWay);
		stopEntity.setStopReason(stopReason);
		
		int flag=stopService.updateStop(stopEntity);
		
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}

}
